<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2020-02-03 13:18:19
 * Desc:
 */

namespace app\base\model\address;

use think\Model;

class UserPostInfo extends Model
{

    // 关闭自动写入时间戳
    protected $autoWriteTimestamp = false;

    protected $table = 'xy_address.xy_user_post_info';

    public static function AdminGetLogisticsList($where, $query_string = '', $page = 1, $page_size = 20)
    {
        $field = 'group.group_id,group.success_time,group.create_time,
        post.log_id,post.address_id,post.post_state,post.receive_name as p_receive_name,post.receive_phone as p_receive_phone,post.address_info,post.post_company_name,post.post_order,post.post_time,post.remark,post.create_time,
        address.receive_name,address.receive_phone,address.postal_code,address.province_name,address.city_name,address.area_name,address.address_detail,
        goods.goods_name,goods.goods_title,goods.goods_sub_title,goods.goods_img_url,goods.need_wish_coin,goods.need_people,goods.people_get_wish_coin,
        platform.platform_name,platform.platform_price,platform.platform_url,
        user.user_id,user.nick_name,user.gender,user.small_avatar_url';
        $query = self::alias('post')
            ->join('xy_address.xy_user_address_info address', 'address.address_id=post.address_id', 'LEFT')
            ->join('xy_user.xy_user user', 'user.user_id=post.user_id')
            ->join('xy_goods.xy_goods_group_info group', 'post.group_id=group.group_id')
            ->join('xy_goods.xy_goods_base_info goods', 'goods.goods_id=group.goods_id')
            ->join('xy_goods.xy_goods_platform_info platform', 'platform.goods_id=goods.goods_id');

        if ($where) {
            $query->where($where);
        }

        if ($query_string) {
            $query->where($query_string);
        }


        $items = $query->field($field)->paginate($page_size)->toArray();

        if ($items && isset($items['data']) && $items['data']) {
            foreach ($items['data'] as &$item) {
                $item['receive_name'] = !empty($item['receive_name']) ? $item['receive_name'] : $item['p_receive_name'];
                $item['receive_phone'] = !empty($item['receive_phone']) ? $item['receive_phone'] : $item['p_receive_phone'];
                if ($item['province_name'] && $item['city_name'] && $item['area_name'] && $item['address_detail']) {
                    $item['address_info'] = $item['province_name'] . ' ' . $item['city_name'] . ' ' . $item['area_name'] . ' ' . $item['address_detail'];
                }
                unset($item['province_name'], $item['city_name'], $item['area_name'], $item['address_detail'], $item['p_receive_name'], $item['p_receive_phone']);
            }
        }

        return $items;
    }

    public static function getAllPostLogIds($where, $query_string = '')
    {
        $query = self::alias('post')
            ->join('xy_user.xy_user user', 'user.user_id=post.user_id')
            ->join('xy_goods.xy_goods_group_info group', 'post.group_id=group.group_id')
            ->join('xy_goods.xy_goods_base_info goods', 'goods.goods_id=group.goods_id');

        if ($where) {
            $query->where($where);
        }

        if ($query_string) {
            $query->where($query_string);
        }


        $items = $query->column('post.log_id');

        return $items;
    }


    public static function thirdPlatformPostState($log_ids, $post_state)
    {
        return self::whereIn('log_id', $log_ids)->update(['post_state' => $post_state]);
    }

    public static function apiGetLogisticsInfo($group_id)
    {
        return self::field('post_company_name,post_order,post_time')
            ->where('group_id', $group_id)
            ->where('post_state', 3)
            ->select();
    }

}